package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianjipms.dto.UserRepairDTO;
import com.tianjipms.entity.Bill;
import com.tianjipms.entity.Repair;
import com.tianjipms.service.BillService;
import com.tianjipms.service.RepairService;
import com.tianjipms.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/api/repair")
public class RepairController {
    @Resource
    private RepairService repairService;
    @Resource
    private BillService billService;
    //查询报修
    @GetMapping("/listAll")
    public Result listAll(@RequestParam(defaultValue = "1") Long pageNum,
                          @RequestParam(defaultValue = "5") Long pageSize){
        IPage<Repair> page = new Page<>(pageNum, pageSize);
        repairService.page(page);
        return Result.ok(page);
    }
    @GetMapping("/listByUserId")
    public Result listByUserId(@RequestParam(defaultValue = "1") Long pageNum,
                               @RequestParam(defaultValue = "5") Long pageSize,
                               @RequestParam Long userId){
        IPage<Repair> page = new Page<>(pageNum, pageSize);
        repairService.findRepairListByUserIdPage(page, userId);
        return Result.ok(page);
    }
    @GetMapping("/listByServiceId")
    public Result listByServiceId(@RequestParam(defaultValue = "1") Long pageNum,
                                  @RequestParam(defaultValue = "5") Long pageSize,
                                  @RequestParam Long serviceId){
        IPage<Repair> page = new Page<>(pageNum, pageSize);
        repairService.findRepairListByServiceIdPage(page, serviceId);
        return Result.ok(page);
    }
    //创建报修单
    @PostMapping("/add")
    public Result addRepair(@RequestBody Repair repair){
        repair.setSendTime(new Date());
        repair.setServiceId(0L);
        repair.setServiceStatus(0);
        repair.setUserStatus(0);
        repair.setPrice(BigDecimal.valueOf(0.00));
        return Result.ok(repairService.save(repair));
    }
    //指定工作人员
    @PostMapping("/binding")
    public Result bindingServiceId(@RequestBody Repair repair){
        return repairService.updateByRepair(repair);
    }
    //修理完成
    @PostMapping("/serviceComplation")
    public Result serviceComplation(@RequestBody Repair repair){
        repair.setServiceTime(new Date());
        return repairService.updateByRepair(repair);
    }
    //用户满不满意
    @PostMapping("/userEvaluate")
    public Result userEvaluate(@RequestBody Repair repair){
        //不满意
        if(repair.getUserStatus() == 2){
            repair.setServiceStatus(0);
            repair.setServiceTime(null);
            repair.setPrice(BigDecimal.valueOf(0));
            return repairService.updateServiceTimeByRepair(repair);
        }else {
            Bill bill = new Bill();
            if(repair.getType() == 0){
                bill.setUserId(1L);
                bill.setMessage("公共维修费");
            }else {
                bill.setUserId(repair.getUserId());
                bill.setMessage("私人维修费");
            }
            bill.setPayeeId(repair.getServiceId());
            bill.setCreateTime(new Date());
            bill.setPrice(repair.getPrice());
            bill.setStatus(0);
            billService.save(bill);
            return repairService.updateByRepair(repair);
        }
    }
    //删除报修单
    @DeleteMapping("/delete/{id}")
    public Result deleteRepair(@PathVariable Long id){
        return Result.ok(repairService.removeById(id));
    }
}
